from django.http import JsonResponse
from django.views.generic import View
from ..utils.literUtils import get_model_serializer_config, type_conversion_dict
from ..serializers.authorInfoSerializer import AuthorCreateSerializer
from ..serializers.authorListSerializer import AuthorListQuerySerializer, AuthorListSerializer

# 作者展示界面配置
class AuthorConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = AuthorListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(AuthorListSerializer)
            table_extra_fields_info = AuthorListSerializer.get_extra_fields_info()
            # print(table_extra_fields_info)
            for extra_fields in table_extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            # print(table_fields)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            cit_hidden_fields = ["tag", "institutions", "departments"]
            sortable_fields = ["name", "zh_name", "country", "city", "cited_count"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in cit_hidden_fields:
                    field["citHidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "author配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)

# 作者新建界面配置
class AuthorAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(AuthorCreateSerializer)
            hidden_fields = []
            cit_hidden_fields = ["tag", "institutions", "departments"]
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in cit_hidden_fields:
                    field["citHidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)